import numpy as np
import pandas as  pd
from sklearn.svm import SVC
import matplotlib.pyplot as plt

x_train = np.random.randn(300,2) #xunlian shuju youzhengyoufu
y_train = np.logical_xor(x_train[:,0]>0,
                         x_train[:,1]>0)

x = np.arange(-3,3,0.1)
y = np.arange(-3,3,0.1)
xx,yy = np.meshgrid(x,y)
xy = np.c_[xx.ravel(),yy.ravel()]

svm = SVC(kernel='rbf')
svm.fit(x_train,y_train)

plt.figure(figsize=(8,8))
plt.scatter(x_train[:,0],x_train[:,1],
            c=y_train,s=30,cmap=plt.cm.Paired,edgecolors='k')
# plt.savefig('svm2.png')
dd = svm.decision_function(xy)
d = dd.reshape(xx.shape)
plt.imshow(d,extent=(xx.min(),xx.max(),yy.min(),yy.max()),
           cmap=plt.cm.PuOr_r) #retu
plt.contour(xx,yy,d) #lunkuotu
plt.savefig('svm3.png')
